package com.wangweixiong.role.controller;

import com.wangweixiong.role.base.BaseController;
import com.wangweixiong.role.service.UserService;
import com.wangweixiong.role.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name="UserControllerGetWWX",urlPatterns="/wwx/user/get")
public class UserControllerGetWWX extends BaseController {
    private UserService userService =new UserServiceImpl();
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        // 专门处理http协议的请求
        servletRequest.setCharacterEncoding("UTF-8");


        HttpServletRequest request= (HttpServletRequest)servletRequest;
        String method = request.getParameter("method");
        // 根据method的不同进入不同方法
        if(method.equals("login")){
            String phone = request.getParameter("username");
            String password = request.getParameter("password");
            Integer userId = userService.findUserIdByPhonePassword(phone, password);
            HttpSession session;
            if(userId!=null){
                session = request.getSession(true);
                session.setAttribute("userId",userId);
            }
            printData(userId!=null,null,500,servletResponse);
        }




    }
}
